package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    public void saveOrUpdate(Product entity) {
        if (entity.getId() == null) {
            productMapper.insert(entity);
        } else {
            productMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    public Product get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public List<Product> listAll() {
        return productMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = productMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Product> products = productMapper.queryList(qo);
        return new PageResult(count, products);
    }

    @Override
    public List<Product> selectAllProduct(Long insureId) {
        return productMapper.selectAllProduct(insureId);
    }
}
